import React from 'react';
import { BrowserRouter as Router, Route, Routes, Navigate } from 'react-router-dom';
import Login from './pages/Login';
import { AuthProvider, useAuth } from './context/AuthContext';
import AdminLayout from './pages/admin/AdminLayout';
import UserLayout from './pages/user/UserLayout';
import PermissionsManagement from './pages/admin/PermissionsManagement';
import VipManagement from './pages/admin/VipManagement';
import VipService from './pages/user/VipService';
import PluginCenter from './pages/user/PluginCenter';
import UserCenter from './pages/user/UserCenter';

// 路由组件
const AppRoutes = () => {
  const { user } = useAuth();

  return (
    <div className="container">
      <Routes>
        {/* 登录页面 */}
        <Route path="/login" element={<Login />} />
        
        {/* 管理员路由 */}
        <Route 
          path="/home" 
          element={
            user?.is_admin ? (
              <AdminLayout />
            ) : (
              <Navigate to="/login" replace />
            )
          }
        >
          <Route index element={<Navigate to="/home/permissions" replace />} />
          <Route path="permissions" element={<PermissionsManagement />} />
          <Route path="vip" element={<VipManagement />} />
        </Route>
        
        {/* 用户路由 */}
        <Route 
          path="/*" 
          element={
            !user?.is_admin ? (
              <UserLayout />
            ) : (
              <Navigate to="/login" replace />
            )
          }
        >
          <Route index element={<Navigate to="/home" replace />} />
          <Route path="home" element={<PluginCenter />} />
          <Route path="profile" element={<UserCenter />} />
          <Route path="vip" element={<VipService />} />
          <Route path="settings" element={<div>设置</div>} />
        </Route>
        
        {/* 默认重定向到登录页 */}
        <Route path="/" element={<Navigate to="/login" replace />} />
        
        {/* 捕获所有其他路由并重定向到登录页 */}
        <Route path="*" element={<Navigate to="/login" replace />} />
      </Routes>
    </div>
  );
};

function App() {
  return (
    <Router>
      <AuthProvider>
        <AppRoutes />
      </AuthProvider>
    </Router>
  );
}

export default App; 